import axios, {
    type AxiosResponse,
    type AxiosRequestConfig,
    type AxiosRequestHeaders,
} from "axios";
import { ElMessage, ElMessageBox } from "element-plus";
import pinia from "../stores/index";

interface ResponseData<T> {
    code: number;
    data: T;
    message: string;
}

// 配置新建一个 axios 实例
const service = axios.create({
    //基础路径
    baseURL: "/mock",
    //超时设置
    timeout: 5000, //代表超时的时间为5S
});

// 添加请求拦截器
service.interceptors.request.use((config: AxiosRequestConfig) => {
    return config;
});

// 添加响应拦截器
service.interceptors.response.use(
    /* 约束一下response */
    async (response: AxiosResponse<ResponseData<any>, any>) => {
        return response.data.data;
    },
    (error) => {
        // 对响应错误做点什么
        return Promise.reject(error);
    }
);

export default service;
